package supporter;

import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by adinlead on 17-7-13.
 */
public class Pagger {
    final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Pagger() {
    }

    /**
     * @param page  页码
     * @param rows  行数
     * @param count 总记录数
     * @param rows  记录主体
     */
    public Pagger(int page, int rows, Long count, List data) {
        this.page = page;
        this.rows = rows;
        this.count = count;
        this.data = data;
        total = (int) Math.ceil((double) count / (double) rows);
    }

    public Pagger(TableItem item, Long count, List data) {
        this.page = item.page();
        this.rows = item.rows();
        this.count = count;
        this.data = data;
        total = (int) Math.ceil((double) count / (double) this.rows);
    }

    private int code = 0;
    /**
     * 页码
     */
    public int page;
    /**
     * 行数
     */
    public int rows;
    /**
     * 总页码
     */
    public int total;
    /**
     * 记录总数
     */
    public long count;
    /**
     * 记录主体
     */
    public List data;

    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        json.put("page", this.page);
        json.put("rows", this.rows);
        json.put("total", this.total);
        json.put("count", this.count);
        json.put("data", this.data);
        json.put("code", this.code);
        json.put("msg", "OK");
        return json;
    }

    @Override
    public String toString() {
        return this.toJSON().toString();
    }
}
